<?php
include "../Utils/alidayu/TopSdk.php";

class AliDaYuSend {
    //代理商给超市补贴时，当达到某个金额时给代理商发送短信
    public static function sendSmsToAgentForSubsidy($mobile, $city, $subsidy){
        try {
            $c = new TopClient;
            $c->appkey = '23433886';
            $c->secretKey = 'd74fcc6ad43662ea3d1f30557ebd3a99';
            $req = new AlibabaAliqinFcSmsNumSendRequest;
            $req->setExtend("123456");
            $req->setSmsType("normal");
            $req->setSmsFreeSignName("翻贝网");
            $req->setSmsParam("{\"city\":\"" . $city . "\",\"sub\":\"" . $subsidy . "\"}");
            $req->setRecNum($mobile);
            $req->setSmsTemplateCode("SMS_89690002");
            $resp = $c->execute($req);
            return true;
        } catch (Exception $e) {
            return false;
        }
    }
    //代理商给超市补贴
    public static function sendSmsToAgentByCurl($mobile, $cityName, $subsidy) {
        $app_key = '23433886'; // APP Key
        $app_secret = 'd74fcc6ad43662ea3d1f30557ebd3a99'; // APP Secret
        $para = [
            'city' => $cityName,
            'sub' => $subsidy
        ];
        $url = 'http://gw.api.taobao.com/router/rest'; // API 地址
        /* POST 参数 */
        $post = array(
            'app_key' => $app_key,
            'format' => 'json',
            'method' => 'alibaba.aliqin.fc.sms.num.send',
            'partner_id' => 'apidoc',
            'sign_method' => 'md5',
            'timestamp' => date('Y-m-d H:i:s'),
            'v' => '2.0',
            'extend' => '',
            'rec_num' => $mobile,
            'sms_free_sign_name' => '翻贝网',
            'sms_param' => json_encode($para),
            'sms_template_code' => 'SMS_89690002',
            'sms_type' => 'normal'
        );

        ksort($post); // 按键名 排序 (大多签名生成的必须步骤)

        /* 按照签名算法 生成签名 */
        $res = '';
        foreach($post as $k => $v){	$res .= $k . $v; }

        $res = str_replace(
            array('/\+/', '/\*/', '/%7E/'),
            array('%20', '%2A', '~'),
            $res
        );
        $post['sign'] = strtoupper(md5($app_secret . $res . $app_secret));

        /* CURL 发送数据 */
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $result = curl_exec($ch);
        curl_close($ch);

        //$json = json_decode($result, true);
        return $result;
    }
}

?>
